//
//  CommitViewController.swift
//  Shien
//
//  Created by AINI on 2019/3/14.
//  Copyright © 2019 尘埃. All rights reserved.
//

import UIKit

class MessageViewController: UBaseViewController {
    
    private var page: Int = 0
    private var limitTime: String = ""
    private var msgList = [MsgModel]()
    private var spinnerName: String?
    
    private lazy var tableView: UITableView = {
        let el = UITableView(frame: .zero, style: .plain)
        el.separatorStyle =  UITableViewCell.SeparatorStyle.none
        el.backgroundColor = UIColor.background
        el.tableFooterView = UIView()
        el.delegate = self
        el.dataSource = self
        el.rowHeight = UITableView.automaticDimension
        el.register(cellType: MessageCell.self)
        el.register(cellType: OrderMsgCell.self)
        el.uHead = URefreshHeader { [weak self] in self?.loadData(more: false) }
        el.uFoot = URefreshDiscoverFooter { [weak self] in self?.loadData(more: true) }
        el.uempty = UEmptyView(text:"暂无数据～") { [weak self] in self?.loadData(more: false) }
        return el
    }()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        loadData(more: false)
    }
    override func configNavigationBar() {
        super.configNavigationBar()
        navigationItem.title = "消息"
        navigationController?.navigationBar.isTranslucent = false
    }
    @objc private func loadData(more: Bool) {
         let index = (more ?  page : 0)
//        ApiLoadingProvider.request(UApi.msgList(params: ["page": index, "pageSize": PAGESIZE]), model: MsgList.self) { [weak self] (returnData) in
//            if ((returnData?.messageList) == nil){
//                self?.tableView.uFoot.endRefreshingWithNoMoreData()
//            } else {
//                self!.page += 1
//                self?.tableView.uempty?.allowShow = true
//                if !more { self?.msgList.removeAll() }
//                self?.msgList.append(contentsOf: returnData?.messageList ?? [])
//
//                self?.tableView.reloadData()
//            }
//            self?.tableView.uFoot.endRefreshing()
//            self?.tableView.uHead.endRefreshing()
//        }
    }
    
    override func configUI() {
        view.addSubview(tableView)
        tableView.snp.makeConstraints{ $0.edges.equalTo(self.view.usnp.edges) }
    }
}

extension MessageViewController: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return msgList.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let model = msgList[indexPath.row] as MsgModel
        if model.orderSn == "" {
         let  cell = tableView.dequeueReusableCell(for: indexPath, cellType: MessageCell.self)
             cell.model = model
             return cell
        }else{
          let  cell = tableView.dequeueReusableCell(for: indexPath, cellType: OrderMsgCell.self)
            cell.model = model
             return cell
        }
    }
}

